﻿@model IEnumerable<TestSys.Models.CourseModel>
@{
    ViewBag.Title = "ManageCourses";
    Layout = "~/Views/Admin/_AdminLayout.cshtml";
}

<script type="text/javascript">
    function destroy(ID) {
        if (confirm("Deleting this course will result into deleting all tests it contains! Sure?")) {
            var form = document.createElement("form");
            form.setAttribute("method", "post");
            form.setAttribute("action", "DeleteCourse");

            var hiddenField = document.createElement("input");
            hiddenField.setAttribute("type", "hidden");
            hiddenField.setAttribute("name", "DestrID");
            hiddenField.setAttribute("value", ID);

            form.appendChild(hiddenField);

            document.body.appendChild(form);
            form.submit();

            /*var url = 'Url.Action("DeleteCourse","Admin")';
            url += '/' + ID;
            location.href = url;*/
        }
    }

    function create() {

        // The rest of this code assumes you are not using a library.
        // It can be made less wordy if you use one.
        var name = window.prompt("Type here new name for course:(1-60 ch)");
        if (name != null && name!="" && name.length<=60) {
            var form = document.createElement("form");
            form.setAttribute("method", "post");
            form.setAttribute("action", "CreateCourse");

            var hiddenField = document.createElement("input");
            hiddenField.setAttribute("type", "hidden");
            hiddenField.setAttribute("name", "NewName");
            hiddenField.setAttribute("value", name);

            form.appendChild(hiddenField);

            document.body.appendChild(form);
            form.submit();
        }
    }

</script>

<h2>ManageCourses</h2>

<table id = "manageTable" rules="groups">
<colgroup span=3 />
<colgroup span=1 />
<tr>
    <th>Name</th>
</tr>
@foreach (var item in Model)
{
    <tr>
        <td>@item.CourseName</td>
        <td>@Html.ActionLink("Edit", "EditCourse", "Admin", new { id = item.CourseID }, null)</td>
        <td>@Html.ActionLink("Add test", "CreateTest", "Admin", new { id = item.CourseID }, null)</td>
        <td><p onclick="destroy('@item.CourseID')">-</p></td>
    </tr>
}
</table>
<div id="CreateLnk">
    <input type="button" value="Create new course" onclick="create()" />
</div>